local skel = fk.CreateSkill{
  name = "kt_ls__chuailing",
}

Fk:loadTranslationTable{
  ["kt_ls__chuailing"] = "揣令",
  [":kt_ls__chuailing"] = "出牌阶段开始或有角色进入濒死的回合结束时，你可以令任意名体力值不小于你的角色各重铸一张牌，"..
  "然后你获得其中点数最大的【闪】，使用其中任意张点数小于之的牌（若没有重铸【闪】，则均可以使用）。",
  ["#kt_ls__chuailing"] = "揣令：你可以令任意名体力值不小于你的角色各重铸一张牌",
  ["#kt_ls__chuailing_ask"] = "重铸一张牌，然后 %src 获得其中点数最大的【闪】，使用其中任意张点数小于之的牌（若没有重铸【闪】，则均可以使用）",
  ["#kt_ls__chuailing_ask2"] = "使用其中任意张点数小于等于 %arg 的牌",
  ["@@kt_ls__chuailing-turn"] = "揣令",

  ["$kt_ls__chuailing1"] = "好梦千秋，淮水畅快流，只说忠奸争斗。",
  ["$kt_ls__chuailing2"] = "宝殿凌霄，谁去逍遥游，争那挥鞭策牛。",
}

skel:addEffect(fk.EnterDying,{
  can_refresh =function (self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"@@kt_ls__chuailing-turn",1)
  end,
})

local function oncost(self, event, target, player, data)
  local room = player.room
  local players = {}
  for _,p in ipairs(room:getAlivePlayers()) do
    if not p:isNude() and p.hp >= player.hp then
      table.insertIfNeed(players,p)
    end
  end
  local chooses = room:askToChoosePlayers(player,{
    min_num = 1,
    max_num = 999,
    targets = players,
    skill_name = skel.name,
    prompt = "#kt_ls__chuailing",
    cancelable = true
  })
  if #chooses > 0 then
    event:setCostData(self,{tos = chooses})
    return true
  end
end

local function onuse(self, event, target, player, data)
  local room = player.room
  --选牌并重铸
  local tos = event:getCostData(self).tos
  room:sortByAction(tos)
  local chos = room:askToJointCards(player,{
    players = tos,
    min_num = 1,
    max_num = 1,
    skill_name = skel.name,
    cancelable = false,
    prompt = "#kt_ls__chuailing_ask:" .. player.id
  })
  local cards = {}
  for _,p in ipairs(tos) do
    if not p.dead then
      room:recastCard(chos[p],p,skel.name)
      table.insertIfNeed(cards,chos[p][1])
    end
  end
  --获得闪，或者返回空
  local n = 0
  local chojink = {}
  for _,cid in ipairs(cards) do
    local c = Fk:getCardById(cid)
    if c.name == "jink" and c.number > n then
      n = c.number
      chojink = {cid}
    elseif c.name == "jink" and c.number == n then
      table.insertIfNeed(chojink,cid)
    end
  end
  if #chojink > 0 then
    room:moveCardTo(chojink[1],Card.PlayerHand,player,fk.ReasonJustMove,skel.name)
    table.removeOne(cards,chojink[1])
    n = Fk:getCardById(chojink[1]).number - 1
  else
    n = 13
  end
  --用牌
  if n < 1 then return end
  room:moveCards({
    ids = cards,
    toArea = Card.Processing,
    moveReason = fk.ReasonPut,
    skillName = skel.name,
    proposer = player
  })
  while #cards > 0 and not player.dead do
    local use = room:askToUseRealCard(player, {
      pattern = ".|A~" .. n .. "|.|^(hand,equip)",
      skill_name = skel.name,
      prompt = "#kt_ls__chuailing_ask2:::" .. n,
      extra_data = {
        expand_pile = cards,
        extraUse = true,
        bypass_times = true,
      },
      cancelable = true,
      skip = true,
    })
    if use then
      room:useCard(use)
      table.remove(cards,table.indexOf(cards,use.card.id))
    else
      break
    end
  end
  room:cleanProcessingArea()
end

skel:addEffect(fk.TurnEnd,{
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player:getMark("@@kt_ls__chuailing-turn") > 0 then
      local room = player.room
      local players = {}
      for _,p in ipairs(room:getAlivePlayers()) do
        if not p:isNude() and p.hp >= player.hp then
          table.insertIfNeed(players,p)
        end
      end
      return #players > 0
    end
  end,
  on_cost = oncost,
  on_use = onuse,
})

skel:addEffect(fk.EventPhaseStart,{
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and player.phase == Player.Play then
      local room = player.room
      local players = {}
      for _,p in ipairs(room:getAlivePlayers()) do
        if not p:isNude() and p.hp >= player.hp then
          table.insertIfNeed(players,p)
        end
      end
      return #players > 0
    end
  end,
  on_cost = oncost,
  on_use = onuse,
})

return skel